#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "mazewidget.h"
#include "mazegenerator.h"

#include <QDialog>
#include <QLabel>
#include <QVector>
#include <QPushButton>
#include <QRandomGenerator>
#include<QMessageBox>

MazeData randommaze1;
Switch sw;
Solution s1;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    //主窗口设置
    this->setFixedSize(900,600);   //设置固定大小
    this->setWindowTitle("迷宫求解系统");    //设置窗口标题

    //迷宫展示窗口设置
    maze1 = new MazeWidget;
    maze1->setWindowTitle("迷宫展示窗口");

    //退出系统
    connect(ui->pushButton_4,&QPushButton::clicked,[=](){this->close();});

    //创建迷宫
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        // 随机创建迷宫
        randommaze1 = mazegenerator::createRandomMaze();
        maze1->setMaze(randommaze1);
        qDebug() << "随机创建 clicked";
        QMessageBox::information(this,"迷宫","成功创建");

    });

    //展示迷宫
    connect(ui->pushButton_5,&QPushButton::clicked,[=](){
        maze1->show(); // 显示迷宫窗口
    });

    //
}


//用算法求解迷宫

void MainWindow::on_pushButton_2_clicked()
{
    sw.switchPhase(randommaze1);
    sw.get_b();
    sw.get_e();

    QVector<QVector<int>> path1(0);
    QVector<QVector<int>> path2(0);

    sw.reset_visited();
    s1.get_route(path1);

    if (!path1.isEmpty()) {
        qDebug() << "find paths" << Qt::endl;
        qDebug() << "All paths:" << Qt::endl;
        for (int i = 0; i < path1.size(); ++i) {
            qDebug() << "Path " << i + 1 << ": ";
            for (int j = 0; j < path1[i].size(); ++j) {
                int index = path1[i][j];
                if (index >= 0 && index < hnode.size() && hnode[index] != nullptr) {
                    qDebug() << hnode[index]->xy.x() << hnode[index]->xy.y() << " ";
                } else {
                    qDebug() << "Invalid node index " << index << Qt::endl;
                }
            }
            qDebug() << Qt::endl;
        }

        sw.reset_visited();
        s1.shortest(path2);
        qDebug() << "Shortest path:" << Qt::endl;
        for (int i = 0; i < path2.size(); ++i) {
            qDebug() << "Path " << i + 1 << ": ";
            for (int j = 0; j < path2[i].size(); ++j) {
                int index = path2[i][j];
                if (index >= 0 && index < hnode.size() && hnode[index] != nullptr) {
                    qDebug() << hnode[index]->xy.x() << hnode[index]->xy.y() << " ";
                } else {
                    qDebug() << "Invalid node index " << index << Qt::endl;
                }
            }
            qDebug() << Qt::endl;
            QString str="查找成功，共有";
            str+=QString::number(path1.size());
            str+="条路！";
            QMessageBox::information(this,"迷宫",str);
        }
    } else {
        qDebug() << "No paths found." << Qt::endl;
        QMessageBox::warning(this,"迷宫","没有路径");
    }
    path3=path1;
    path4=path2;
}


void MainWindow::showPath() {
    // 创建一个新的QWidget作为路径展示页面
    QWidget* pathDisplay = new QWidget();
    QVBoxLayout* layout = new QVBoxLayout();

    pathDisplay->resize(600, 400);
    pathDisplay->setWindowTitle("路径展示窗口");
    // 设置页面标题
    QLabel* titleLabel = new QLabel("路径展示");
    titleLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(titleLabel);

    // 创建迷宫视图

    // 假设这里实例化了一个迷宫视图类
    maze1->setParent(pathDisplay);
    layout->addWidget(maze1);
    maze1->show();
    // 添加展示路径的按钮
    QPushButton* switchButton = new QPushButton("切换");
    QPushButton* shortestButton = new QPushButton("最短路径");
    QPushButton* exitButton = new QPushButton("退出");

    // 添加按钮的点击事件
    connect(switchButton, &QPushButton::clicked, [=]() {
        // 切换展示路径的逻辑
        static int currentPathIndex = 0;
        qDebug()<<"switch"<<path3.size();
        if (!path3.isEmpty()) {
            if(currentPathIndex<path3.size()){
            if(currentPathIndex==0){
            QVector<int> currentPath = path3[currentPathIndex];
                QVector<int> lastPath;
            lastPath.resize(0);
                maze1->paintPath(lastPath,currentPath);
            }
            else{
                QVector<int> currentPath = path3[currentPathIndex];
                QVector<int> lastPath=path3[currentPathIndex-1];
                maze1->paintPath(lastPath,currentPath);
            }
            maze1->show();            // 在迷宫中涂绿当前路径
            qDebug()<<"switch"<<path3.size();
            }
            currentPathIndex = currentPathIndex + 1;

        }
    });

    connect(shortestButton, &QPushButton::clicked, [=]() {
        // 展示最短路径的逻辑
        if (!path4.isEmpty()) {
            QVector<int> shortest = path4[0];
            QVector<int> lastPath;
            lastPath.resize(0);
            maze1->paintPath(lastPath,shortest); // 在迷宫中涂绿最短路径
            qDebug()<<"switch";
        }
    });

    connect(exitButton, &QPushButton::clicked, [=]() {
        // 退出当前页面的逻辑
        pathDisplay->close(); // 关闭当前的路径展示页面
    });

    layout->addWidget(switchButton);
    layout->addWidget(shortestButton);
    layout->addWidget(exitButton);

    pathDisplay->setLayout(layout);
    pathDisplay->show();
}

void MainWindow::on_pushButton_3_clicked()
{
    showPath();
}

MainWindow::~MainWindow()
{
    delete ui;
}
